<?php

class Jf_Tree_Adapter_Category extends Jf_Tree_Node {

    protected static $_adapter = 'category';

    public function setCatName ($cat_name) {
        if (null !== $cat_name &&  ! is_string ($cat_name)) {
            throw new Jf_Tree_Exception ('Invalid argument: $cat_name must be a string or null');
        }
        $this -> _label = $cat_name;
        return $this;
    }

    public function setCatId ($id = null) {
        if (null !== $id &&  ! is_string ($id) &&  ! is_numeric ($id)) {
            throw new Jf_Tree_Exception ('Invalid argument: $id must be a string, number or null');
        }
        $this -> _id = null === $id ? $id : (string) $id;
        return $this;
    }

    public function setParentId ($parent_id = null) {
        if (null !== $parent_id &&  ! is_string ($parent_id) &&  ! is_numeric ($parent_id)) {
            throw new Jf_Tree_Exception ('Invalid argument: $parent_id  must be a string, number or null');
        }
        $this -> _parent_id = null === $parent_id ? $parent_id : (string) $parent_id;
        return $this;
    }

    public function isActive ($recursive = false) {
        $dispatcher = Jf_Controller_Dispatcher::getInstance ();
        $cat_id = $dispatcher -> getRequest () -> getParam ('id', null);
        if (isset ($cat_id) && ($cat_id == $this -> getId ())) {
            $this -> _active = TRUE;
            return true;
        } else {
            return parent::isActive ($recursive);
        }
    }

    public function getHref () {
        return '/catalog/category?id=' . $this -> getId ();
    }

    public function toArray () {
        return array_merge (parent::toArray (), array('uri' => $this -> getHref ()));
    }

}
